package com.shitu.soulemo.a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.shitu.base.BaseFragment;
import com.shitu.base.net.API;
import com.shitu.base.net.model.Banners;
import com.shitu.base.net.model.result.PHomePageTab;
import com.shitu.base.net.service.ProviderService;
import com.shitu.base.net.service.RequireService;
import com.shitu.soulemo.R;
import com.shitu.soulemo.utils.AccountHelper;
import com.shitu.soulemo.utils.GlideImageLoader;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by majie on 2018/3/20.
 */

public class FuwuFragment extends BaseFragment {
    private TextView text;
    private XRecyclerView list;
    private Banner banner;
    private ProviderAdapter pAdapter;
    private RequireAdapter rAdapter;
    private ArrayList<String> images = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wupin_fragment, null);
        list = view.findViewById(R.id.list);
        banner = view.findViewById(R.id.banner);
        text = view.findViewById(R.id.text);
        initViews();
        return view;
    }

    public void initViews() {
        text.setText("附近物品的信息");
        banner.setImageLoader(new GlideImageLoader());
        pAdapter = new ProviderAdapter(this);
        rAdapter = new RequireAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        list.setLayoutManager(layoutManager);
        list.setPullRefreshEnabled(false);

        if (AccountHelper.isRequire()) {
            list.setAdapter(rAdapter);
            getRData();
        } else {
            list.setAdapter(pAdapter);
            getPData();
        }
    }

    private void getPData() {
        API.send(ProviderService.getHomePageTab(0, 1), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                final PHomePageTab tab = JSON.parseObject(data, PHomePageTab.class);

                //banner
                List<Banners> banners = tab.getData().getList().getBanners();
                images.clear();
                for (int i = 0; i < banners.size(); i++) {
                    images.add(banners.get(i).getImgUrl());
                }
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            banner.setImages(images);
                            banner.start();

                            //list
                            pAdapter.setData(tab.getData().getList().getTabs());
                            pAdapter.notifyDataSetChanged();
                        }
                    });
                }
            }
        });
    }

    private void getRData() {
        API.send(RequireService.getHomePageTab(0, 1), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                final PHomePageTab tab = JSON.parseObject(data, PHomePageTab.class);

                //banner
                List<Banners> banners = tab.getData().getList().getBanners();
                for (int i = 0; i < banners.size(); i++) {
                    images.add(banners.get(i).getImgUrl());
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        banner.setImages(images);
                        banner.start();

                        //list
                        rAdapter.setData(tab.getData().getList().getTabs());
                        rAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
}
